<?php
namespace app\supermanager\model;
use think\Model;
use think\Session;
/**
* 管理员管理
*/
class Managers extends Model
{
	use \app\common\traits\model\TraitModel;
   	protected $table = "managers";
   	protected static $_loginuser = '';
   
   
   /**
    * 获取器
    */
    public function getStatusAttr($value)
    {
        $status = [0=>'锁定',1=>'解锁'];
        return $status[$value];
    }
   
   /**
     * [accountLogin 账号密码方式登录]
     * @param  [type]  $account     [description]
     * @param  [type]  $password    [description]
     * @param  boolean $remember_me [description]
     * @return [type]               [description]
     */
    public function managerLogin($account,$password)
    {
        $this->clearManagerSession();
		
        if ($managerData=$this->getManagerInfo($account,$password)) {
        	if($managerData->getData('status')===0){
				$this->error = lang('该账号已被锁定');
				return false;
			}
            $this->setManagerSession($managerData);
            return true;
        }else{
            $this->error = lang('用户名或密码错误');
			return false;
        }
    }
	
   /**
    *根据账号和密码获取管理员信息 
    */
    public function getManagerInfo($account,$password)
    {
    	$checkIf['account'] = $account;
		$checkIf['password'] = $password;
    	return $this->where($checkIf)->find();
    }
   /**
    * 把用户信息存入session
    */
   public function setManagerSession($managerData)
   {
	   $session_id = session_id();
	   session('super_manager.session_id',$session_id);
	   session('super_manager.account',$managerData->account);
	   session('super_manager.real_name',$managerData->real_name);
	   session('super_manager.id',$managerData->id);
   }
   
   /**
    * 清空manager的session
    */
   private function clearManagerSession()
   {
   		session::clear();
   }
   
   /**
    * 退出登录
    */
    public function logout(){
    	session::clear();
		return true;
    }
   /**
     * [checkLogin 判断是否已经登陆]
     * @return [type] [description]
     */
    public function checkLogin()
    {
        if (session('?super_manager')) {
            $session_id = $this->getLoginManagerInfo(session('super_manager.id'), 'session_id');
            if ($session_id !== session('super_manager.session_id')){
                self::$_loginuser = '';
                $this->logout();
            }
        }
        return session('?super_manager') ? true : false;    
    }
   
   /**
     * 根据用户id获取用户信息
     * @param  string $user_id [description]
     * @param  string $field   [description]
     * @return [type]          [description]
     */
    protected static function getLoginManagerInfo($manager_id, $field = '')
    {
        if (empty($manager_id)) {
            return ;
        }
        if (empty(self::$_loginuser)) {
            self::$_loginuser = self::where('id',$manager_id)->find();
        }
        if ($field) {
            if (isset(self::$_loginuser[$field])) {
                return self::$_loginuser[$field]; 
            }
            return false;
        }
        return self::$_loginuser;
    }
	
    /**
     * 获取登录用户信息
     * @param  string $field [description]
     * @return [type]        [description]
     */
    public static function getLoginUserInfo($field = '')
    {
        return self::getLoginManagerInfo(session('super_manager.id'), $field);
    }
   
   /**
    * 更新数据库信息
    * @param array $manager_info_array
    * @param id $manager_id
    */
    public function updateInfo($manager_info_array,$manager_id){
    	try {
    		if(is_array($manager_id)){
    			$this->where('id', 'in', $manager_id)->update($manager_info_array);
    		}else{
    			$this->where(['id'=>$manager_id])->update($manager_info_array);
    		}
    	} catch (\Exception $e) {
    	    return 0;
    	}
		return 1;
    }
	/**
	 * 获取管理员信息
	 * @param array $manager_info_array
	 */
	public function getAllManagerInfo($manager_info_array){
		return $this->where($manager_info_array)->find();
	}
	
	/**
	 * 管理员信息列表
	 */
	public function getCaravansLists($querys = [])
    {
        return $this->getInfoList($querys);
    }
	/**
	 * 添加管理员信息
	 * @param array $manager_info_array
	 */
	public function addManagerInfo($manager_info_array){
		return $this->data($manager_info_array)->save();
	} 
	
	/**
	 * 删除管理员
	 * @param array $manager_array_id
	 */
	public function delManager($manager_array_id){
	    return $this->destroy($manager_array_id);
	}
	
}















